#!/usr/bin/env python
# -*- coding: utf-8 -*-

from docx import Document
from docx.shared import Inches
# 打开已经存在的文档
#document = Document("e:\pythonroot\demo\模板.docx")

# 显示每段的内容
#for p in document.paragraphs:
    #print(p.text)

# 保存
#document.save("e:\pythonroot\demo\模板2.docx")

def redTable (filename):
 document = Document(filename)
 allTables = document.tables

 # 修改指定位置的内容
 # allTables[0].cell(0, 1).text = '□优秀　　　□好　　　　□良好　　　■一般　　　□较差　　　□很差'

 #document.save("e:\pythonroot\demo\修改好.docx")
 #return
# 处理表格
 for aTable in allTables:
   for i in range(len(aTable.rows)):
    for j in range(len(aTable.columns)):
     print(i,j)  # 表格数据所在的位置
     print(aTable.cell(i, j).text)



def readSpecTable(filename, specText):
 document = Document(filename)
 paragraphs = document.paragraphs
 allTables = document.tables


 specText = specText.encode('utf-8').decode('utf-8')
 for aPara in paragraphs:
  if aPara.text == specText:
   ele = aPara._p.getnext()
   while (ele.tag != '' and ele.tag[-3:] != 'tbl'):
    ele = ele.getnext()
   if ele.tag != '':
    for aTable in allTables:
     if aTable._tbl == ele:
      for i in range(len(aTable.rows)):
       for j in range(len(aTable.columns)):
        print(aTable.cell(i, j).text)

if __name__ == '__main__':
 redTable('e:\pythonroot\demo\模板.docx')